package com.stylefeng.guns.modular.system.task;

import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.http.client.methods.HttpGet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.stylefeng.guns.core.common.constant.state.ZgpType;
import com.stylefeng.guns.core.util.HttpClientUtil;
import com.stylefeng.guns.modular.system.constants.ImportContants;
import com.stylefeng.guns.modular.system.model.Gdtj;
import com.stylefeng.guns.modular.system.model.Gpdata;
import com.stylefeng.guns.modular.system.model.ZgdData;
import com.stylefeng.guns.modular.system.service.IGdtjService;
import com.stylefeng.guns.modular.system.service.IGpdataService;

@Service
public class ZgpImportTask {
	
	private Logger log = LoggerFactory.getLogger(ZgpImportTask.class);
	
	private static final HttpClientUtil clientUtil = new HttpClientUtil();
	
	@Autowired
    private IGpdataService gpdataService;
	
	@Autowired
	private IGdtjService gdtjService;
	
    public static final Map<String,String> dates = new HashMap<String,String>() {{  
        put("03-31","");  
        put("06-30","");  
        put("09-30","");  
        put("12-31","");
    }};   
	
	public void execute(boolean isHoliday,ZgpType type) {
		if(isSearchDay()){
			String day = "2018-06-30";//DateTimeKit.format(new Date(), "yyyy-MM-dd");
			List<Gpdata> gpdatas = gpdataService.selectList(null);
			for(Gpdata gpdata : gpdatas){
				String url = ImportContants.getFormatGdPathStr(gpdata.getgDm(),day,type);
				HttpGet httpGet = HttpClientUtil.getGetRequest();
				try {
					httpGet.setURI(new URL(url).toURI());
					String json = clientUtil.executeAndGetWithFailedRetry(httpGet, 6,10001);
					JSONObject jsonObj = JSON.parseObject(json);
					parseData(jsonObj.getString("data"));
				} catch (Exception e) {
					log.error("get gudong data is error!" + e);
				}finally{
					httpGet.abort();
				}
			}
		}
	}
	
	private void parseData(String json){
		if(StringUtils.isNotEmpty(json)){
			List<ZgdData> channelItemList = JSON.parseArray(json,ZgdData.class);
			for(ZgdData entity : channelItemList){
				
				Gdtj gdtj = new Gdtj();
				gdtj.setgRdate(entity.getRDATE());
				gdtj.setgSharehdname(entity.getSHAREHDNAME());
				gdtj.setgDm(entity.getSCODE());
				gdtj.setgSharestype(entity.getSHARESTYPE());
				gdtj  = gdtjService.selectGdTjByGdtj(gdtj);
				if(gdtj == null){
					gdtj = new Gdtj();
					gdtj.setgBdbl(entity.getBDBL());
					gdtj.setgBz(entity.getBZ());
					gdtj.setgCompanycode(entity.getCOMPANYCODE());
					gdtj.setgDm(entity.getSCODE());
					gdtj.setgRank(entity.getRANK());
					gdtj.setgRdate(entity.getRDATE());
					gdtj.setgSharehdname(entity.getSHAREHDNAME());
					gdtj.setgSharehdnum(entity.getSHAREHDNUM());
					gdtj.setgSharehdratio(entity.getSHAREHDRATIO());
					gdtj.setgSharehdtype(entity.getSHAREHDTYPE());
					gdtj.setgSharestype(entity.getSHARESTYPE());
					gdtj.setgSname(entity.getSNAME());
					gdtj.setgZb(entity.getZB());
					gdtjService.insert(gdtj);
				}
			}
		}
	}
	
	private boolean isSearchDay(){
		String day = "2018-06-30";//DateUtil.formatDate(new Date(), "yyyy-MM-dd");
		return dates.get(StringUtils.substringAfter(day, "-")) != null;
	}
}
